package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工程报装-批量更新用户信息中的营业区域
 * 入参：pjMeterIdList,businessArea
 * 1. 通过pjMeterIdList查出目前所有的立户信息
 * 2. 批量更新用户信息中的businessArea
 */
public class PjMeterUpdateBusinessArea implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(),PjMeterQuery.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);

            List<String> pjMeterIdList = pjMeterQuery.getPjMeterIdList();
            if (pjMeterIdList != null && pjMeterIdList.size() > 0) {
                // 1. 批量查出所有立户信息
                List<PjMeter> pjMeterList = pjMeterMapper.getListByIdList(pjMeterIdList);
                // 2. 聚合所有临时用户id
                List<String> userInfoTempIdList = pjMeterList.stream().map(PjMeter::getUserInfoTempId).collect(Collectors.toList());
                // 3. 批量更新临时用户的businessArea
                userInfoMapper.batchUpdateBusinessArea(userInfoTempIdList,pjMeterQuery.getBusinessArea(), UserBean.USERTEMP);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "批量更新用户信息中的营业区域成功", null);
        } catch (Exception e) {
            logger.error("批量更新用户信息中的营业区域失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"批量更新用户信息中的营业区域失败",null);
        }
    }
}
